home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 419 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.9 KB  |  159 lines

  1. Path: news.mel.aone.net.au!OzEmail!usenet
  2. From: tedjones@voyager.co.nz (Ted Jones)
  3. Newsgroups: comp.lang.c++
  4. Subject: Problem with polymorphism.
  5. Date: Thu, 04 Jan 1996 14:10:43 GMT
  6. Organization: Ted Jones Motor Co Ltd.
  7. Message-ID: <4cgjkr$fcr@oznet07.ozemail.com.au>
  8. NNTP-Posting-Host: ts1p08.net.hamilton.voyager.co.nz
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. Yo All!  (Email reply's please I don't read regularly enough)
  12.  
  13. I have a problem with polymorphisim. :(
  14.  
  15. Here's some code to chew on.
  16.  
  17. #ifndef _KEY_HPP_
  18. #define _KEY_HPP_
  19.  
  20. class TKey {
  21. private:
  22.    // In derived classes there should be a "key1" variable defined
  23. here.
  24. public:
  25.    // TKey's destructer is virtual as there may be several different
  26. types of keys
  27.    // with different allocation methods and hence different
  28. de-allocation methods.
  29.    virtual ~TKey(void) {};
  30.    // Returns 0 if keys are equal, 1 if key2 is greater and -1 if key2
  31. is l
  32.    virtual int Compare(TKey *key2) = 0;
  33. };
  34.  
  35. At this level TKey is abstract and knows nothing of it's descendents.
  36. Here is it's decendent.
  37.  
  38. class iKey: public TKey {
  39. private:
  40.    int key;
  41. public:
  42.    iKey(int i) { key = i; }
  43. //   virtual ~iTKey(void) {};
  44.    virtual int Compare(TKey *key2);
  45.    virtual int GetKey(void) { return key; }
  46. };
  47.  
  48. And here is it's implemented pure virtual function Compare();
  49.  
  50. int iKey::Compare(TKey *key2)
  51. {
  52.    if(key == key2->GetKey()){
  53.       return 0;
  54.    } else {
  55.       if(key < key2->GetKey()){
  56.          return 1;
  57.       } else {
  58.          return -1;
  59.       }
  60.    }
  61. }
  62.  
  63. This doesn't work tho because Compare is passed a TKey param and TKey
  64. doesn't have the GetKey() member.   I don't think it should because it
  65. doesn't have data to "get".  And if I do declare it there I run into
  66. other problems.  Now if you understand what I'm trying to do and know
  67. how to do it properly please share it with me.  
  68.  
  69. Basically Compare is called by some other class.  Passing in a pointer
  70. to another object derived from the TKey class.  Through polymorphisim
  71. it should call the right GetKey().  Admittedly it would only be sane
  72. to pass in a pointer to a iKey becuase only iKeys should be compared
  73. with iKeys.  But I don't know what would happen if I was to declare
  74. the original pure virtual function as: 
  75.  
  76. virtual int Compare(void) = 0; 
  77.  
  78. and then in all derived classes declare it as:
  79.  
  80. virtual int Compare(iKey *key2); 
  81.  
  82. etc.  Can I overload virtual functions like that safely?  But then I
  83. also run into the problem with the other class calling
  84. TKey->Compare(tkey);  Because the other class only knows about TKey
  85. and if I decalare it with (void) the other class wont compile.  I am
  86. basically stuck as to what I can do. PLEASE HELP ME. Sob sob. :_(  I'm
  87. loving C++ so far.  It's just when I do weird shit (which is just
  88. plain not possible in C) I get stuck quite badly.
  89.  
  90. BTW here's the "other class" I was talking about.
  91.  
  92. class TBTree {                // An incomplete btree class.  Yeah 
  93.                     // yeah I know there are lots of class
  94. private:                // libs out there that do this for me.
  95.    typedef struct btree_node {        // But I'm learning and do it all the
  96.       TKey *key;            // hard way to be a better programmer.
  97.       TStorable *data;
  98.       btree_node *left, *right;
  99.    } TBTree_node;
  100.    
  101.    TBTree_node *btree_root;
  102.    TBTree_node *current;
  103.    
  104.    int size;
  105.    bool success;
  106.    
  107.    void RecurseInsert(TBTree_node *root, TBTree_node *node);
  108. public:
  109.    TBTree(void) { btree_root = NULL; size = 0; success = TRUE;}
  110.    virtual ~TBTree(void);
  111.  
  112.    virtual void Insert(TKey *key, TStorable *data);
  113.  
  114.    int Size(void) { return size; }
  115.    bool Success(void) { return success; }
  116. };
  117.  
  118. And here's where it calls Compare();
  119.  
  120. void TBTree::RecurseInsert(TBTree_node *root, TBTree_node *node)
  121. {
  122.    if (root == NULL){
  123.       root = node;
  124.    } else {
  125.       switch((root->key->Compare(node->key))){
  126.          case 0:
  127.             success = TRUE;
  128.             root = node;                        // Overwrite existing
  129. nodes.
  130.             break;
  131.          case 1:
  132.             RecurseInsert(root->right, node);   // Insert on right.
  133.             break;
  134.          case -1:
  135.             RecurseInsert(root->left, node);    // Insert on left.
  136.             break;
  137.          default:
  138.             // Somehthing is amiss.  Abnormal Termination.
  139.             assert(((root->key->Compare(node->key)) != 0) &&
  140.                    ((root->key->Compare(node->key)) != 1) &&
  141.                    ((root->key->Compare(node->key)) != -1)
  142.                   );
  143.             break;
  144.       }
  145.    }
  146. }
  147.  
  148. - Dreamer/Reality
  149. --- timEd-B10
  150.  * Origin: æ The Digital Dream's lonely point. (3:774/1500.1)
  151.  
  152. Ted Jones (Ted Jones Motor Co Ltd.) | Aki Enterprise (Manager)  Ph: (045)625-0877
  153. PO Box 74, Taypo, New Zealand       | 20-27 Honmokuhara,    Office: (045)621-3031
  154. Phone: (07)-378-7494                | Naka-ku, Yokohama 231   Hand: (030)27-99191
  155. Fax: (07)-378-7478                  | Japan                    Fax: (045)622-1420
  156. Cellular: (025)-960-399             |              After Hours Fax: (045)253-3239
  157.  ...-...    'Exporters of Quality Used Japanese Assembled Vehicles'      ...-...
  158.  
  159.